package com.xuecheng.order.service;

import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.framework.domain.task.XcTaskHis;
import com.xuecheng.framework.exception.CustomExcetionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.order.dao.XcTaskHisRepository;
import com.xuecheng.order.dao.XcTaskRepository;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class XcTaskService {
    @Autowired
    XcTaskRepository xcTaskRepository;
    @Autowired
    XcTaskHisRepository xcTaskHisRepository;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * @Description: 分页查询
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    public List<XcTask> findList(Date date, int size) {
        Pageable pageable = new PageRequest(0, 10);
        Page<XcTask> page = xcTaskRepository.findByUpdateTimeBefore(pageable, date);
        List<XcTask> content = page.getContent();
        return content;
    }

    /**
     * @Description: 更新时间
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */

    public void xcTaskUpdate(XcTask xt) {
        if (xt == null) {
            return;
        }

        Optional<XcTask> xcTask = xcTaskRepository.findById(xt.getId());
        if (xcTask.isPresent()) {
            XcTask task = xcTask.get();
            task.setUpdateTime(new Date());
            xcTaskRepository.save(task);
        }
    }

    /**
     * @Description: 发送消息
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    public void sendSuccess(XcTask xcTask) {
        if(xcTask==null){
            return;
        }
        rabbitTemplate.convertAndSend(xcTask.getMqExchange(), xcTask.getMqRoutingkey(), xcTask);
    }
    /**
    * @Description: 更新版本号
    * @Param:
    * @return:
    * @Author: Mr.Yuan
    * @Date:
    */
    @Transactional
    public int updateVersion(String id,Integer version){
        int i = xcTaskRepository.updateVersion(id, version);
        return i;
    }

    /**
    * @Description:  添加历史记录，删除原先任务
    * @Param:
    * @return:
    * @Author: Mr.Yuan
    * @Date:
    */
    @Transactional
    public void saveAndDelete(String taskId){
        Optional<XcTask> optionalXcTask = xcTaskRepository.findById(taskId);
        if(!optionalXcTask.isPresent()){
            throw CustomExcetionCast.getCusException(CommonCode.SERVER_ERROR);
        }
        XcTask xcTask = optionalXcTask.get();
        XcTaskHis xcTaskHis=new XcTaskHis();
        BeanUtils.copyProperties(xcTask,xcTaskHis);
        xcTaskHisRepository.save(xcTaskHis);
        xcTaskRepository.deleteById(taskId);

    }

}